package com.xiaoyao.novel.proxy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.util.AutoConfigIP;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.FileUtil;
import com.xiaoyao.novel.util.HttpUtil;
/**
 * 代理IP设置类
 * 
 * @author Administrator
 * 
 */
public class ProxyUtil {
	static Logger logger = Logger.getLogger(ProxyUtil.class);
	public static String proxyIpFile = "proxyIp.txt"; // 代理IP 文件.
	// private static final String username = "域\\用户";
	// private static final String password = "密码";
	private static String host = "174.36.178.106"; //    
	private static String port = "80";
	private static final List<String> ipProxyList = new ArrayList<String>();

	static Random random = new Random();
	static Properties props = System.getProperties();
	static {
		// 读取代理
		initProxy();
	}

	private static void initProxy() {
		try {
			String content = FileUtil.readFile(proxyIpFile);
			String[] ipProxys = content.split("\\s+");
			ipProxyList.addAll(Arrays.asList(ipProxys));
		} catch (IOException e) {
			logger.error(e);
		}
	}
	/**
	 * 随机获取一个代理IP
	 */
	private static void readIp() {
		if (!ClassUtil.isEmpty(ipProxyList)) {
			int index = random.nextInt() % ipProxyList.size();
			String ipProxy = ipProxyList.get(index);
			String[] strs = ipProxy.split(":");
			if (strs.length == 2) {
				host = strs[0];
				port = strs[1];
			}
		}
	}
	/**
	 * 设置代理
	 */
	public static void startProxy() {
		readIp();// 随机 读取一个代理
		props.setProperty("proxySet", "true");
		props.setProperty("http.proxyHost", host.trim());
		props.setProperty("http.proxyPort", port.trim());
	}
	/**
	 * 关闭代理
	 */
	public static void endProxy() {
		props.setProperty("proxySet", "false");
	}
	public static boolean needProxy(String crawlId) {
		return CrawlProxy.needProxy(crawlId);
	}
	// ///////////////////////////////////////////
	public static void main(String[] args) {
		testProxyIp();
	}
	/**
	 * 测试代理IP是否可用
	 */
	public static void testProxyIp() {
		String localIp = AutoConfigIP.getLocalIP(); // 本地IP
		for (String ipProxy : ipProxyList) {
			String[] strs = ipProxy.split(":");
			if (strs.length == 2) {
				host = strs[0];
				port = strs[1];
			}
			props.setProperty("proxySet", "true");
			props.setProperty("http.proxyHost", host.trim());
			props.setProperty("http.proxyPort", port.trim());
			try {
				HttpUtil httpUtil = new HttpUtil("UTF-8");
				String urlStr = "http://www.tianfengfeiye.com/testip.jsp";
				String page = httpUtil.getPage(urlStr);
				logger.info("返回页面:" + page);
				if (page.contains(localIp)) { // IP 不可用
					logger.info(host + ":" + port + "  不可用");
				} else { // 可用
					logger.info(host + ":" + port + "  可用");
				}

			} catch (IOException e) {
				logger.error(e);
			}
		}
	}

}
